#ifndef Com_Mod
#define Com_Mod
#include <boost/asio.hpp>
#include <qmutex.h>
#include <qthread.h>
#define PORT "COM3"

using namespace::boost::asio;  
using namespace std;	


class Communicator : public QObject{
	Q_OBJECT

public:
	
	int  X, Y, Fire;
	bool IsConnected;
	//bool NewData;
	//bool FirstTime;
	//bool Done;
	HANDLE CommDone; 
	HANDLE NewData;


	//io_service object is needed for creating a serial port, it is used to read an write data to an io device (serial port).
	io_service io;
	serial_port *port;

	
	//Union is reserves a memory space for the biggest variable(struct) in the union and this way we can send the data over in bytes.
	//This is possible because all the variables in the union have the same memory space witout any seperation.
	//Now we can send the struct in the union in byte parts over since the are one and the same memory space.
	//Note that only one variable type can be accesed at aany given time, since there is only data for one variable in the reserverd union memory space.
	union SendPacked{
		struct test{
			int32_t Y;
			int32_t X;
		    int32_t AngleX;
            int32_t AngleY;
			int32_t Fire;
		} Struc;

		unsigned char bytes[20];
	} SendData;

		union RecvPacked{
		struct test{
			int32_t Y;
			int32_t X;
		    int32_t AngleX;
            int32_t AngleY;
			int32_t Fire;
		} Struc;

		unsigned char bytes[20];
	} RecvData;

	//Default constructor creates an instance of a serial port with the given parameters (io service object and comport name).

		Communicator()
		{};

	~Communicator(){};

	public slots:
	void run();

	//Class functions
	int Setup(QString CurrentCom);
	void Send(int X, int Y, int AngleX, int AngleY, int Fire);
	void Recieve();

private:
	int x, y;
};

#endif
